﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VOL;
using BLL;

namespace Shopper
{
    public partial class frmNguoiDungManager : DevExpress.XtraEditors.XtraForm
    {
        NguoiDungBL nguoiDungBL = new NguoiDungBL();
        public frmNguoiDungManager()
        {
            InitializeComponent();
        }

        private void frmNguoiDungManager_Load(object sender, EventArgs e)
        {
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = nguoiDungBL.GetData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (frmNguoiDungForm frm = new frmNguoiDungForm())
            {
                NguoiDung objNguoiDung = new NguoiDung();
                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    objNguoiDung.TenTruyCap = frm.TenTruyCap;
                    objNguoiDung.MatKhau = frm.MatKhau;
                    objNguoiDung.HoTen = frm.HoTen;

                    nguoiDungBL.InsertData(objNguoiDung);
                    XtraMessageBox.Show("Đã thêm mới người dùng thành công !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int MaNguoiDung = (int)gridView1.GetFocusedRowCellValue("MaNguoiDung");

            using (frmNguoiDungForm frm = new frmNguoiDungForm())
            {
                DataTable dt = nguoiDungBL.GetDataById(MaNguoiDung);
                frm.TenTruyCap = dt.Rows[0]["TenTruyCap"].ToString();
                frm.MatKhau = dt.Rows[0]["MatKhau"].ToString();
                frm.HoTen = dt.Rows[0]["HoTen"].ToString();

                NguoiDung objNguoiDung = new NguoiDung();
                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    objNguoiDung.TenTruyCap = frm.TenTruyCap;
                    objNguoiDung.MatKhau = frm.MatKhau;
                    objNguoiDung.HoTen = frm.HoTen;
                    objNguoiDung.MaNguoiDung = MaNguoiDung;

                    nguoiDungBL.UpdateData(objNguoiDung);
                    XtraMessageBox.Show("Đã thay đổi người dùng thành công !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = XtraMessageBox.Show("Bạn có muốn xóa bản ghi này không ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                int MaNguoiDung = (int)gridView1.GetFocusedRowCellValue("MaNguoiDung");
                nguoiDungBL.DeleteData(MaNguoiDung);
                FillData();
            }
        }
    }
}